module Adder_Substractor_Signed8bit(A, B, Sel, S, Cout);
  
  input [7:0] A, B;
  input Sel;
  output [7:0] S;
  output Cout;
  wire [7:0] wMA, wMB, wavA, wavB, wavApB, wavAmB, wavMApB, wavMAmB;
  wire [2:0] wSel;
  wire w1, w2, w3, w4, w5, w6;
  
  Full_Adder_8bit F1(~A, 8'b00000001, 8'b00000000, wMA, w1);
  Full_Adder_8bit F2(~B, 8'b00000001, 8'b00000000, wMB, w2);
  
  MUX_2x1_8bit M1(A, wMA, A[7], wavA);
  MUX_2x1_8bit M2(B, wMB, B[7], wavB);
  
  Adder_Substractor_8bit A1(wavA, wavB, 8'b00000000, wavApB, w3);
  Adder_Substractor_8bit A2(wavA, wavB, 8'b00000001, wavAmB, w4);
  
  Full_Adder_8bit F3(~wavApB, 8'b00000001, 8'b00000000, wMavApB, w5);
  Full_Adder_8bit F4(~wavAmB, 8'b00000001, 8'b00000000, wMavAmB, w6);
  
  assign wSel[2]=Sel;
  assign wSel[1]=A[7];
  assign wSel[0]=B[7];

  MUX_8x1_8bit M3(wavApB, wavAmB, wMavAmB, wMavApB, wavAmB, wavApB, wavMApB, wavMAmB, wSel, S);
  MUX_8x1_8bit_M4(w3, w4, w6, w5, w4, w3, w5, w6, wSel, Cout);
  
endmodule

module Half_Adder(A, B, S, C);
  
  input A, B;
  output C, S;
  
  xor X1(S, A, B);
  and A1(C, A, B);
  
endmodule

module Full_Adder(A, B, Cin, S, Cout);
  
  input A, B, Cin;
  output S, Cout;
  wire w1, w2, w3;
  
  Half_Adder H1(A, B, w1, w2);
  Half_Adder H2(w1, Cin, S, w3);
  
  or O1(Cout, w2, w3);
  
endmodule

module Full_Adder_8bit(A, B, Cin, S, Cout);
  
  input [7:0] A, B;
  input Cin;
  output [7:0] S;
  output Cout;
  wire [7:0] wC;
  
  Full_Adder F1(A[0], B[0], Cin, S[0], wC[0]);
  Full_Adder F2(A[1], B[1], wC[0], S[1], wC[1]);
  Full_Adder F3(A[2], B[2], wC[1], S[2], wC[2]);
  Full_Adder F4(A[3], B[3], wC[2], S[3], wC[3]);
  Full_Adder F5(A[4], B[4], wC[3], S[4], wC[4]);
  Full_Adder F6(A[5], B[5], wC[4], S[5], wC[5]);
  Full_Adder F7(A[6], B[6], wC[5], S[6], wC[6]);
  Full_Adder F8(A[7], B[7], wC[6], S[7], wC[7]);
  
  assign Cout=wC[7];
  
endmodule

module Adder_Substractor_8bit(A, B, Sel, S, Cout);
  
  input [7:0] A, B;
  input Sel;
  output [7:0] S;
  output Cout;
  wire [7:0] wb;
  
  xor X1(wb[7], B[7], ~Sel);
  xor X2(wb[6], B[6], ~Sel);
  xor X3(wb[5], B[5], ~Sel);
  xor X4(wb[4], B[4], ~Sel);
  xor X5(wb[3], B[3], ~Sel);
  xor X6(wb[2], B[2], ~Sel);
  xor X7(wb[1], B[1], ~Sel);
  xor X8(wb[0], B[0], ~Sel);
  
  Full_Adder_8bit A1(A, wb, Sel, S, Cout);
  
endmodule
*/